package com.wenbo.wenbo.kafka;


import cn.hutool.json.JSONUtil;
import com.wenbo.wenbo.dto.GPSDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
@Slf4j
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;



    public static final String YG_T_HAV_GPS = "CAR_BASE_INFO";
    public static final String YG_T_HAV_GPS2 = "EMP_QUASI_DRIVING";

    //
    public static final String TOPIC_GROUP1 = "YG_T_HAV_GPS.YWB";


    public void send(int i,int k) throws InterruptedException {
        for (int j = 0; j < i; j++) {
            log.info("准备发送消息为："+j);
            ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(YG_T_HAV_GPS, setData(k));
            future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
                @Override
                public void onFailure(Throwable throwable) {
                    //发送失败的处理
                    log.info(YG_T_HAV_GPS + " - 生产者 发送消息失败：" + throwable.getMessage());
                }
                @Override
                public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
                    //成功的处理
                    //log.info(TOPIC_TEST + " - 生产者 发送消息成功：" + stringObjectSendResult.toString());
                    log.info(YG_T_HAV_GPS + " - 生产者 发送消息成功：");
                }
            });
            Thread.sleep(200);
        }
    }

    public void send2(int i,int k) throws InterruptedException {
        for (int j = 0; j < i; j++) {
            log.info("准备发送消息为："+j);
            ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(YG_T_HAV_GPS2, setData(k));
            future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
                @Override
                public void onFailure(Throwable throwable) {
                    //发送失败的处理
                    log.info(YG_T_HAV_GPS2 + " - 生产者 发送消息失败：" + throwable.getMessage());
                }
                @Override
                public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
                    //成功的处理
                    //log.info(TOPIC_TEST + " - 生产者 发送消息成功：" + stringObjectSendResult.toString());
                    log.info(YG_T_HAV_GPS2 + " - 生产者 发送消息成功：");
                }
            });
            Thread.sleep(200);
        }
    }
    private String setData(int k){
        List<GPSDto> data=new ArrayList<>();
        for (int i = 0; i < k; i++) {
            GPSDto gps=new GPSDto();
            gps.setCarnumber("川A000"+i);
            gps.setCarstate("行驶中-"+i);
            gps.setGpstime(System.currentTimeMillis());
            gps.setDirection("北-"+i);
            gps.setLatitue(""+i);
            gps.setLongtitue(""+(i+100));
            gps.setSpeed((i*10)+"");
            gps.setSjgxrq(System.currentTimeMillis());

            data.add(gps);
        }

        return JSONUtil.toJsonStr(data);
    }
}